﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using O.M.Production.Application.IService;
using O.M.Production.Application.Service;
using O.M.Production.Domain.Entity;
using O.M.Production.ErrorCodeEnum;
using System.Linq.Expressions;

namespace O.M.Production.Read.API.Controllers
{
    /// <summary>
    /// Api控制器-工艺流程
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class Read_Process_FlowController : ControllerBase
    {
        private readonly I_process_flow_Service _Process_Flow_Service;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="process_Flow_Service"></param>
        public Read_Process_FlowController(I_process_flow_Service process_Flow_Service)
        {
            _Process_Flow_Service = process_Flow_Service;
        }

        /// <summary>
        /// 分页查询显示-工艺流程
        /// </summary>
        /// <param name="process_flow_id_name">工艺流程编号或者名称</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">页码</param>
        /// <returns>统一返回格式</returns>
        [HttpGet]
        public async Task<IActionResult> Get_process_flow_Page(string? process_flow_id_name, int pageSize, int pageIndex)
        {
            try
            {
                var list =await _Process_Flow_Service.Get_process_flow_Page(process_flow_id_name, pageSize, pageIndex);
                return Ok(list);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 导出列表到Excel
        /// </summary>
        /// <returns>Excel文件</returns>
        [HttpGet]
        public async Task<IActionResult> Post_Derive_Excel()
        {
            try
            {
                var list = await _Process_Flow_Service.GetShowInforAsync();
                var ms = new MemoryStream();
                ms.SaveAs(list);
                ms.Seek(0, SeekOrigin.Begin);
                return new FileStreamResult(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                {
                    FileDownloadName = "工艺流程.xlsx"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 加载所有工艺流程
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult process_flow()
        {
            try
            {
                var list = _Process_Flow_Service.GetShowInfor();
                return Ok(list);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
